package com.wikidot.designpatterns.builder;

import java.util.Properties;

/**
 * The client application which defines which builder is to be used, provides
 * "parsing input" and delegates the construction of the product to the wrapping
 * class.
 */
public class Client {
	public static void main(String[] args) {
		/*
		 * For ease of use, define "input string" as Properties object.
		 */
		Properties[] products = { new Properties(), new Properties() };

		/*
		 * There will be two products.
		 */
		products[0].setProperty("id", "123456789");
		products[0].setProperty("description", "The best product in the world");
		products[0].setProperty("tags", "10kg+fragile+handle with care");

		products[1].setProperty("id", "ABC456789");
		products[1]
				.setProperty("description", "The worst product in the world");
		products[1].setProperty("tags", "1kg+keep in dry place");

		LabelCreationDirector director = new LabelCreationDirector();
		Label label = null;

		/*
		 * It is the client who decides which builder to be used. Let's infer
		 * the concrete builder from ID attribute.
		 */
		for (Properties p : products) {
			if (p.getProperty("id").matches("\\d.*")) {
				// If id starts with letter, make a label for company one
				label = director.createLabel(new CompanyOneLabelBuilder(), p);
			} else if (p.getProperty("id").matches("\\D.*")) {
				// If id starts with letter, make a label for company two
				label = director.createLabel(new CompanyTwoLabelBuilder(), p);
			}
			// Print the label
			label.printLabel();
		}
	}
}
